/**
 * 
 */
package com.bolbachchan.common.ui;

import java.io.Serializable;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import com.bolbachchan.common.ui.ScreenMessage.ScreenMessageType;

/**
 * @author Chaitanya
 * 
 */
@Component
public class MessageUtils implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -3001741892608495644L;

    @Autowired
    private MessageSource messageSource;

    /**
     * 
     */
    public MessageUtils() {
	// TODO Auto-generated constructor stub
    }

    /**
     * @param screenMessageContainer
     * @param errors
     * @param clearMessages
     */
    public void translateErrors(ScreenMessageContainer screenMessageContainer, List<? extends ObjectError> errors,
	    boolean clearMessages) {
	if (clearMessages) {
	    screenMessageContainer.getMessages().clear();
	}
	for (ObjectError error : errors) {
	    String code = getMessage(error.getCode(), error.getArguments());
	    ScreenMessage message = new ScreenMessage(ScreenMessageType.ERROR, code);
	    screenMessageContainer.addMessage(message);
	}
    }

    /**
     * @param screenMessageContainer
     * @param result
     * @param clearMessages
     */
    public void translateErrors(ScreenMessageContainer screenMessageContainer, BindingResult result,
	    boolean clearMessages) {
	translateErrors(screenMessageContainer, result.getAllErrors(), clearMessages);
    }

    /**
     * @param codeToFind
     * @param params
     * @return
     */
    public String getMessage(String codeToFind, Object[] params) {
	String returnMessage = messageSource.getMessage(codeToFind, params, "??" + codeToFind + "??",
		Locale.getDefault());
	return returnMessage;
    }

    /**
     * @param screenMessageContainer
     * @param message
     * @param params
     * @param screenMessageType
     */
    public void addMessage(ScreenMessageContainer screenMessageContainer, String message, Object[] params,
	    ScreenMessageType screenMessageType) {
	ScreenMessage sm = new ScreenMessage();
	sm.setMessageType(screenMessageType);
	sm.setMessage(getMessage(message, params));
	screenMessageContainer.addMessage(sm);
    }

    /**
     * @param screenMessageContainer
     * @param screenMessage
     */
    public void addMessage(ScreenMessageContainer screenMessageContainer, ScreenMessage screenMessage) {
	screenMessageContainer.addMessage(screenMessage);
    }

    /**
     * @param screenMessageContainer
     * @param message
     * @param screenMessageType
     */
    public void addMessage(ScreenMessageContainer screenMessageContainer, String message,
	    ScreenMessageType screenMessageType) {
	addMessage(screenMessageContainer, message, null, screenMessageType);
    }

    /**
     * @param screenMessageType
     * @param message
     * @return
     */
    public DefaultOutput getNewDefaultOutputWithMessage(ScreenMessageType screenMessageType, String message) {
	return getNewDefaultOutputWithMessage(screenMessageType, message, null);
    }

    /**
     * @param screenMessageType
     * @param message
     * @param params
     * @return
     */
    private DefaultOutput getNewDefaultOutputWithMessage(ScreenMessageType screenMessageType, String message,
	    Object[] params) {
	DefaultOutput output = new DefaultOutput();
	addMessage(output, message, params, screenMessageType);
	return output;
    }
}
